#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import unittest
import HTMLTestRunner
from tools import handle_request
from utili import LogConfigYaml
import logging
import logging.config


class GetUserDetail(unittest.TestCase):
    def setUp(self):
        self.run = handle_request.RequestHandler()
        LogConfigYaml.setup_logging()
        logger = logging.getLogger('server')  # 维护一个全局日志对象
        logger.error('debug log test1rttfdsf')

    def test_user_detail_01(self):
        url = 'http://59.56.77.23:8202/user/detail'
        params = {

        }

        res = self.run.visit("get", url, params=params)


        self.assertEqual('{"status":-1,"msg":"用户编号、手机号、邮箱不能同时为空","data":null,"page":null}',res.text)
        print(res.text)

    def test_user_detail_02(self):
        url = 'http://59.56.77.23:8202/user/detail'
        params = {
            "userId": "4"
        }

        res = self.run.visit("get", url, params=params)
        self.assertEqual('{"status":-1,"msg":"用户信息获取失败：未找到指定用户","data":null,"page":null}', res.text)
        print(res.text)

    def test_user_detail_03(self):
        url = 'http://59.56.77.23:8202/user/detail'
        params = {
            "userId": "f",
            "mobile": '',
            "email": '',
        }

        res = self.run.visit("get", url, params=params)
        self.assertEqual('{"status":-1,"msg":"用户编号无效","data":null,"page":null}', res.text)
        print(res.text)

    def test_user_detail_04(self):
        url = 'http://59.56.77.23:8202/user/detail'
        params = {
            "userId": '',
            "mobile": '1',
            "email": '2',
        }

        res = self.run.visit("get", url, params=params)
        self.assertIn('status":1,"msg":"成功"',res.text)
        print(res.text)


if __name__ == '__main__':
     path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


     #unittest.main()
     report = os.path.join(path+'/report/result.html')
     print(report)
     file = open(report, "wb")
     # 创建测试套件
     testsuit = unittest.TestSuite()
     testload = unittest.TestLoader()
     # 括号内传入的是类名，会自动找到以test开头全部的用例
     # 用例以例表形式存储
     case = testload.loadTestsFromTestCase(GetUserDetail)
     testsuit.addTests(case)

     case_path = os.path.join(path+'/testCases/usermanage')

     print(case_path)
     discover = unittest.defaultTestLoader.discover(case_path, pattern="*.py", top_level_dir=None)
     print(discover)

     run = HTMLTestRunner.HTMLTestRunner(stream=file,
                                         title="接口自动化测试报告",
                                         description="用例执行结果")
     run.run(testsuit)
     file.close()

